import 'dart:convert';

class VideoListModel {
  List<VideoModel>? list;

  VideoListModel({this.list});

  factory VideoListModel.fromRawJson(String str) =>
      VideoListModel.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory VideoListModel.fromJson(Map<String, dynamic> json) => VideoListModel(
    list: json["list"] != null
        ? List<VideoModel>.from(json["list"].map((x) => VideoModel.fromJson(x)))
        : null,
  );

  Map<String, dynamic> toJson() => {
    "list": list != null
        ? List<dynamic>.from(list!.map((x) => x.toJson()))
        : null,
  };
}

class VideoModel {
  ///当前剧的ID
  int? videoId;

  /// 短剧名称
  String? name;
  String? description;
  String? imgUrl;
  List<String>? tags;

  ///最后观看的剧集的ID，用于查询
  int? lastWatchEpisodesId = 0;

  ///当前剧的ID
  int? id;

  ///短剧总集数
  int? videoNum;

  ///短剧排序ID
  int? sortId;

  List<EpisodeModel>? episodeList;
  int? isCollect;
  int? isLike;
  int? episodeId;
  String? url; // 新增的url字段

  VideoModel({
    this.videoId,
    this.name,
    this.description,
    this.imgUrl,
    this.tags,
    this.lastWatchEpisodesId,
    this.id,
    this.videoNum,
    this.sortId, // 构造函数中添加sortId参数
    this.episodeList,
    this.isCollect,
    this.isLike,
    this.episodeId,
    this.url,
  });

  factory VideoModel.fromRawJson(String str) =>
      VideoModel.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory VideoModel.fromJson(Map<String, dynamic> json) => VideoModel(
    videoId: json["video_id"],
    name: json["name"],
    description: json["description"],
    imgUrl: json["img_url"],
    tags: json["tags"] != null
        ? List<String>.from(json["tags"].map((x) => x.toString()))
        : null,
    lastWatchEpisodesId: json["last_watch_episodes_id"],
    id: json["id"],
    videoNum: json["video_num"],
    sortId: json["sort_id"] ?? 5, // 从JSON中解析sort_id字段，默认值为5
    episodeList: json["episode_list"] != null
        ? List<EpisodeModel>.from(
            json["episode_list"].map((x) => EpisodeModel.fromJson(x)),
          )
        : null,
    isCollect: json["is_collect"],
    isLike: json["is_like"],
    episodeId: json["episode_id"],
    url: json["url"],
  );

  Map<String, dynamic> toJson() => {
    "video_id": videoId,
    "name": name,
    "description": description,
    "img_url": imgUrl,
    "tags": tags != null ? List<String>.from(tags!.map((x) => x)) : null,
    "last_watch_episodes_id": lastWatchEpisodesId,
    "id": id,
    "video_num": videoNum,
    "sort_id": sortId ?? 5, // 将sort_id字段写入JSON，默认值为5
    "episode_list": episodeList != null
        ? List<dynamic>.from(episodeList!.map((x) => x.toJson()))
        : null,
    "is_collect": isCollect,
    "is_like": isLike,
    "episode_id": episodeId,
    "url": url,
  };
}

///剧集内容
class EpisodeModel {
  ///剧集排序ID(客户端不用)
  int? sortId;

  /////封面图
  String? imgUrl;

  /////播放url
  String? url;

  /////解锁剧集所需金币
  int? coin;

  ///是否需要VIP
  int? isVip;

  ///剧集ID
  int? id;

  ///当前剧ID
  int? videoId;

  ///是否已解锁
  int? isUnlock;

  EpisodeModel({
    this.sortId,
    this.imgUrl,
    this.url,
    this.coin,
    this.isVip,
    this.id,
    this.videoId,
    this.isUnlock,
  });

  factory EpisodeModel.fromRawJson(String str) =>
      EpisodeModel.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory EpisodeModel.fromJson(Map<String, dynamic> json) => EpisodeModel(
    sortId: json["sort_id"],
    imgUrl: json["img_url"],
    url: json["url"],
    coin: json["coin"],
    isVip: json["is_vip"],
    id: json["id"],
    videoId: json["video_id"],
    isUnlock: json["is_unlock"],
  );

  Map<String, dynamic> toJson() => {
    "sort_id": sortId,
    "img_url": imgUrl,
    "url": url,
    "coin": coin,
    "is_vip": isVip,
    "id": id,
    "video_id": videoId,
    "is_unlock": isUnlock,
  };
}
